package com.dyylearn.servlet.annotation;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import java.util.HashMap;

/**
 * 模拟一把Tomcat是如果通过@WebServLet（urLPatterns={"/ok1"，"/ok2"}）
 * 来装载一个ServLet的
 */
public class TestAnnotation {
    private static final HashMap<String, Object> hm = new HashMap<>();
    public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        //1．首先要得到扫描的包路径 io，进而得到类的全路径
        String classAllPath = "com.dyylearn.servlet.annotation.OkServlet";
        //2.得到OkServlet的class对象
        Class<?> aClass = Class.forName(classAllPath);
        //3.通过class对象，得到Annotation
        WebServlet annotation = aClass.getAnnotation(WebServlet.class);
        System.out.println(annotation);
        String[] strings = annotation.urlPatterns();
        for(String url : strings) {
            System.out.println("url=" + url);
        }
        //如果匹配url，如果是第一次，tomcat就会创建一个OkServlet实例，放入到hashmap
        Object instance = aClass.newInstance();
        System.out.println("instance=" + instance);

        hm.put("OkServlet", instance);
        System.out.println(hm);
    }
}
